package com.gem.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

import org.apache.log4j.Logger;

public class DBHelper {
	private static  Logger log=Logger.getLogger(DBHelper.class.getName());
//	private static String strUrl="jdbc:sqlserver://localhost:1433;";
//	private static String strLogin="databaseName=MyBook v2.0;user=sa;password=321324;";
	private static Map<String,String> data=XMLHelper.readXML();
	static{
		
	    //String strDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
		try{
			Class.forName(data.get("driver_class"));
		}catch(ClassNotFoundException ex){
			log.error(ex);
		}
		
	}
	
	public static Connection getConnection(){
		Connection con=null;
		try{
			con=DriverManager.getConnection(data.get("url")+"user="+data.get("username")+";password="+data.get("password")+";");
		}
		catch(SQLException ex){
			log.error(ex);
		}
		return con;
	}
	public static void closeAll(Connection con,PreparedStatement sta,ResultSet res){
		if(con!=null){
			try{
				con.close();
				}
				catch(SQLException ex){
					log.error(ex);
				}
		}
		if(sta!=null){
			try{
				sta.close();
				}
				catch(SQLException ex){
					log.error(ex);
				}
		}if(res!=null){
			try{
				res.close();
				}
				catch(SQLException ex){
					log.error(ex);
				}
		}
	}
	
}
